package com.yky.utils;

import com.yky.dao.SerialDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 业务聚合 获取自增序列
 *1。执行 CREATE SEQUENCE ORDERS_SERIAL_NO
 *    START WITH 1000
 *    INCREMENT BY 1
 *    NOMAXVALUE;
 * 2.获取自增序号：SELECT ORDERS_SERIAL_NO.NEXTVAL FROM DUAL;
 */
@Component
public class SerialUtils {
    @Resource
    private SerialDao serialMapper;
    @Resource
    private RedisUtils redisUtils;
    /**
     * 数据库获取自增序列
     * @param serialName
     * @return
     */
    public String serial(String serialName){
        if (StringUtils.isBlank(serialName)){
            throw new IllegalArgumentException("获取自增序号，入参不能为空");
        }
        return serialMapper.selectSerialNo(serialName);
    }

    /**
     * redis获取自增序列
     * @param key
     * @param increment
     * @return
     */
    public String serial(String key, long increment){
        if (StringUtils.isBlank(key)){
            throw new IllegalArgumentException("获取自增序号，入参不能为空");
        }
        long incr = redisUtils.incr(key, increment);
        return String.valueOf(incr);
    }
}
